package ast;

import symbol.SymbolTable;
import symbol.astPreprocessContext;

public class LValStatement extends Statement {
	// LValStatement(其中EXP_STMT比较特殊，没有左值)
	// DECL_STMT, ASSIGN_STMT, SELF_ADD_STMT, SELF_MINU_STMT, EXP_STMT, VOID_STMT
	
	public enum AssignType {
		// = += -= *= /= %=
		NORMAL, ADD, MINU, MULT, DIV, MOD
	}

	public AssignType assignType;

	// 是否常量，针对声明语句DECL_STMT，默认不是
	public Boolean isConst = false;
	// DataSignature类，左值(EXP_STMT不填就行)
	public AstItem LVal;
	// Expression类，右值(如果是自增、自减，右值不填就行)
	public AstItem RVal;

	public String toString() {
		String result = "";
		if (type == StatementType.DECL_STMT) {
			//（未考虑重载函数赋值到变量上，以及不支持类属性的初始化）
			if (isConst == true) {
				result += "const ";
			}
			assert (LVal instanceof PrimaryDataSignature) || (LVal instanceof ComplexDataSignature);

			if (LVal instanceof PrimaryDataSignature) {
				// 非数组
				result += ((PrimaryDataSignature) LVal).getDeclString() + " " + LVal.toString();
				if (((PrimaryDataSignature) LVal).dataType.equals("string")) {
					result += "[100]";
				}
			} else {
				// 数组
				result += ((ComplexDataSignature) LVal).toString();
			}
			if (RVal != null) result += " = " + RVal.toString() + ";";
			else result += ";";
		} else if (type == StatementType.ASSIGN_STMT) {
			assert (LVal != null) && (RVal != null);
			if (assignType == AssignType.NORMAL) {
				result += LVal.toString() + " = " + RVal.toString();
			} else if (assignType == AssignType.ADD) {
				result += LVal.toString() + " += " + RVal.toString();
			} else if (assignType == AssignType.MINU) {
				result += LVal.toString() + " -= " + RVal.toString();
			} else if (assignType == AssignType.MULT) {
				result += LVal.toString() + " *= " + RVal.toString();
			} else if (assignType == AssignType.DIV) {
				result += LVal.toString() + " /= " + RVal.toString();
			} else {
				assert assignType == AssignType.MOD;
				result += LVal.toString() + " %= " + RVal.toString();
			}
			result += ";";
		} else if (type == StatementType.SELF_ADD_STMT) {
			result += LVal.toString() + "++;";
		} else if (type == StatementType.SELF_MINU_STMT) {
			result += LVal.toString() + "--;";
		} else if (type == StatementType.EXP_STMT) {
			result += RVal.toString() + ";";
		} else {
			assert type == StatementType.VOID_STMT;
			result += ";";
		}
		return result;
	}
	
	public void preprocess(AstItem father) {
		// 如果是声明语句，则填入符号表
//		System.out.println(this.type);
		if (this.type == StatementType.DECL_STMT) {
//			System.out.println(((PrimaryDataSignature) LVal).name);
			astPreprocessContext.getContext();
			assert (LVal instanceof PrimaryDataSignature) || (LVal instanceof ComplexDataSignature);
			if (LVal instanceof PrimaryDataSignature) astPreprocessContext.addSymbolInfo((PrimaryDataSignature) LVal);
			// 数组声明
			else {
				ComplexDataSignature d = (ComplexDataSignature) LVal;
				assert d.dataOperated instanceof PrimaryDataSignature;
				int dimCount = d.operation.size();
				String type = ((PrimaryDataSignature) d.dataOperated).dataType;
				((ComplexDataSignature) LVal).dataType = type + "_" + String.valueOf(dimCount);
				astPreprocessContext.addSymbolInfo_array(((ComplexDataSignature) LVal));
			}
		}
		if (LVal != null) LVal.preprocess(this);
		if (RVal != null) RVal.preprocess(this);
	}

	@Override
	public String toJsonString() {
		String json = "{";
		json += "\"Type\": \"Statement-" + type.toString() + "\",";
		json += "\"AssignType\": \"" + (assignType != null ? assignType.toString() : "") + "\",";
		json += "\"IsConst\": \"" + (isConst != null ? isConst : "") + "\",";
		json += "\"LVal\": " + (LVal != null ? LVal.toJsonString() : "\"\"") + ",";
		json += "\"RVal\": " + (RVal != null ? RVal.toJsonString() : "\"\"");
		json += "}";
		return json;
	}
}
